﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_infomation_article_tag_detail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string layerid = Request["layerid"];
            this.PopedomValidate(layerid,QUERY);
            ColumnBll cbll = new ColumnBll();
            this.headers.Text = cbll.LayeridColumnConNaiv(layerid);
            string url = "article_tag_detail.aspx?TAG_ID=" + Request.Params["TAG_ID"] + "&layerid=" + Request.Params["layerid"];
            this.headers.Text += "<img src='../images/subnav_dot.gif' width='9' height='9' />&nbsp;&nbsp;<a href='" + url + "'>标签详细信息</a>&nbsp;&nbsp";

            IList clist = new ArrayList();
            clist.Add(cbll.GetColumn(37));
            clist.Add(cbll.GetColumn(38));
            clist.Add(cbll.GetColumn(137));
            clist.Add(cbll.GetColumn(138));
            clist.Add(cbll.GetColumn(40));


            this.CheckBoxList1.DataSource = clist;
            this.CheckBoxList1.DataTextField = "Classname";
            this.CheckBoxList1.DataValueField = "Classid";
            this.CheckBoxList1.DataBind();

            string TAG_ID = Request.Params["TAG_ID"];
            this.DropDownList2.Text = "黑色";
            this.paneltotal.Visible = false;
            if (TAG_ID != null && TAG_ID.Trim() != "")
            {
                CmsArticleTagBll catb = new CmsArticleTagBll();
                CmsArticleTagModel catm = catb.GetTagModel(int.Parse(TAG_ID));
                this.TextBox1.Text = catm.TagName;
                Session["Name"] = catm.TagName;
                bool bl = PopedomValidateHtml(layerid, PUB);
                if (bl)
                {
                    this.state.Visible = true;
                    if (catm.State == 1)
                    {
                        this.RadioButton1.Checked = true;
                    }
                    else
                    {
                        this.RadioButton2.Checked = true;
                    }
                }
                else
                {
                    this.state.Visible = false;
                }
                if (catm.HotsType != null) { this.DropDownList1.Text = catm.HotsType; }
                if (catm.TagColor != null) { this.DropDownList2.Text = catm.TagColor; }
                this.paneltotal.Visible = true;
                this.TextBox2.Text = catm.ClickNum.ToString();
                string tid = catm.ClassIds.ToString().Trim();
                if (tid != null && tid.Trim() != "")
                {
                    string[] tids = tid.Split(',');
                    for (int i = 0; i < tids.Length; i++)
                    {
                        for (int j = 0; j < CheckBoxList1.Items.Count; j++)
                        {
                            string us = tids[i].ToString();
                            if (this.CheckBoxList1.Items[j].Value.ToString() == us)
                            {
                                this.CheckBoxList1.Items[j].Selected = true;
                            }
                        }
                    }
                }
                if (catm.IsHots == 1)
                {
                    this.RadioButton3.Checked = true;
                }
                else
                {
                    this.RadioButton4.Checked = true;
                }
            }
        }
    }

    //提交按钮
    protected void Button2_Click(object sender, EventArgs e)
    {
        string TAG_ID = Request.Params["TAG_ID"];
        if (TAG_ID == null || TAG_ID.Trim() == "")
        {
            if (JudgmentRepeat() == 0)
            {
                this.PopedomValidate(layerid,ADD);
                int ts = insert();
                string logcontent;
                logcontent = "添加标签--" + this.TextBox1.Text.ToString().Trim();
                this.insertLog(logcontent);
                Response.Write("<script>alert('添加成功!')</script>");
                Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                return;

            }
            else
            {
                Response.Write("<script>alert('名称重复!请重新输入!')</script>");
            }
        }
        else
        {
            if (this.TextBox1.Text.ToString().Trim() == Session["Name"].ToString())
            {
                bool ts = update();
                if (ts)
                {
                    this.PopedomValidate(layerid,MOD);
                    string logcontent;
                    logcontent = "标签名称--" + Session["Name"].ToString() + "修改为--" + this.TextBox1.Text.ToString().Trim();
                    this.insertLog(logcontent);
                    Response.Write("<script>alert('修改成功!')</script>");
                    Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                    return;
                }
            }
            else
            {
                if (JudgmentRepeat() == 0)
                {
                    bool ts = update();
                    if (ts)
                    {
                        this.PopedomValidate(layerid,MOD);
                        string logcontent;
                        logcontent = "标签名称--" + Session["Name"].ToString() + "修改为--" + this.TextBox1.Text.ToString().Trim();
                        this.insertLog(logcontent);
                        Response.Write("<script>alert('修改成功!')</script>");
                        Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('名称重复!请重新输入!')</script>");
                }
            }
        }
        
    }
    //返回按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
        return;
    }
    //添加方法
    private int insert()
    {
        CmsArticleTagModel catm = new CmsArticleTagModel();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        if (this.RadioButton1.Checked == true)
        {
            catm.State = 1;
        }
        else
        {
            catm.State = 2;
        }
        if (this.RadioButton3.Checked == true)
        {
            catm.IsHots = 1;
        }
        else
        {
            catm.IsHots = 2;
        }
        catm.HotsType = DropDownList1.Text.ToString().Trim();
        string IDS = "";
        if (CheckBoxList1.SelectedIndex != -1)
        {
            foreach (ListItem li in CheckBoxList1.Items)
            {
                if (li.Selected)
                {
                    IDS += li.Value.ToString() + ",";
                }
            }
            IDS = IDS.Substring(0, IDS.Length - 1);
        }
        catm.ClassIds = IDS;
        catm.UserId = GetSessionUsersModel().Aid;
        catm.TagColor = DropDownList2.Text;
        int ts = catb.Add(catm);
        saveIndexHtml(catm);
        return ts;
    }

    private void saveIndexHtml(CmsArticleTagModel catm)
    {
        if (catm.IsHots == 1 && catm.State == 1)
        {//发布,生成首页
            if (catm.ClassIds != null && catm.ClassIds != "")
            {
                MenuModel mm = new MenuModel();
                CmsArticleBll cabll = new CmsArticleBll();
                string[] cs = catm.ClassIds.Split(',');
                for (int i = 0; i < cs.Length; i++)
                {
                    if (cs[i] != null && cs[i] != "")
                    {
                        cabll.SaveColumnHtml(mm.GetColumn(int.Parse(cs[i])));
                    }
                }
            }
        }
    }
    //修改方法
    private bool update()
    {
        string TAG_ID = Request.Params["TAG_ID"];
        CmsArticleTagModel catm = new CmsArticleTagModel();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        catm.TagId = int.Parse(TAG_ID.Trim());
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        if (this.RadioButton1.Checked == true)
        {
            catm.State = 1;
        }
        else
        {
            catm.State = 2;
        }
        if (this.RadioButton3.Checked == true)
        {
            catm.IsHots = 1;
        }
        else
        {
            catm.IsHots = 2;
        }
        catm.HotsType = DropDownList1.Text.ToString().Trim();
        string IDS = "";
        if (CheckBoxList1.SelectedIndex != -1)
        {
            foreach (ListItem li in CheckBoxList1.Items)
            {
                if (li.Selected)
                {
                    IDS += li.Value.ToString() + ",";
                }
            }
            IDS = IDS.Substring(0, IDS.Length - 1);
        }
        catm.ClassIds = IDS;
        //catm.ClickNum = int.Parse(this.TextBox2.Text.ToString().Trim());
        catm.TagColor = DropDownList2.Text;
        bool bo = catb.Update(catm);
        saveIndexHtml(catm);
        return bo;
    }
    //判断添加名字是否重复
    private int JudgmentRepeat()
    {
        CmsArticleTagModel catm = new CmsArticleTagModel();
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        IList ls = catb.GetTag(catm);
        if (ls.Count==0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}
